package daoimpl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import bean.Exam;
import dao.ExamDao;
import util.Projectdbpool;

public class ExamDaoImpl implements ExamDao {

	PreparedStatement preparedStatement;
	ResultSet resultSet;
	List<Exam> list = new ArrayList<>();

	@Override
	public List<Exam> queryexam() {
		String loginsql = "select * from examtable";
		preparedStatement = Projectdbpool.excutePreparedStatement(loginsql);

		try {

			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Exam exam = new Exam();
				exam.setExamid(resultSet.getInt("examid"));
				exam.setExamname(resultSet.getString("examname"));
				System.out.println(resultSet.getString("examname")+"1111111111111111111111111111111111111111111");
				exam.setStarttime(resultSet.getString("starttime"));
				exam.setExamcreator(resultSet.getString("examcreator"));
				exam.setExampaper(resultSet.getString("exampaper"));
				exam.setAutostart(resultSet.getString("autostart"));
				exam.setStarted(resultSet.getString("started"));
				exam.setFinished(resultSet.getString("finished"));
				exam.setArchived(resultSet.getString("archived"));
				exam.setCleaned(resultSet.getString("cleaned"));
				exam.setExamining(resultSet.getString("examining"));
				list.add(exam);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Projectdbpool.close();
		}

		return list;
	}

	@Override
	public boolean startexam(Exam exam) {
		String checkstartexamsql = "select count(*) as examing from examtable where examining = ?";
		preparedStatement = Projectdbpool.excutePreparedStatement(checkstartexamsql);
		int count = 0;
		try {
			preparedStatement.setString(1, "yes");
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				count = resultSet.getInt("examing");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Projectdbpool.close();
		}
		if (count >= 1) {
			System.out.println("已有考试正在进行!");
			return false;
		} else {
			String startexamsql = "update examtable set started = ?,examining = ? where examname = ? and examcreator = ?";
			preparedStatement = Projectdbpool.excutePreparedStatement(startexamsql);
			int result = 0;
			try {
				preparedStatement.setString(1, "yes");
				preparedStatement.setString(2, "yes");
				preparedStatement.setString(3, exam.getExamname());
				preparedStatement.setString(4, exam.getExamcreator());
				result = preparedStatement.executeUpdate();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				Projectdbpool.close();
			}
			if (result > 0) {
				System.out.println("考试已开始");
			}
			return true;
		}
	}

	@Override
	public int[] examinfo() {
		int[] studentinfo = new int[3];

		String studentinfosql = "select count(*) as attend,count(ipbinding) as login,count(submittime) as submit from studenttable inner join examtable on studenttable.examname=examtable.examname where examtable.examining=?";
		preparedStatement = Projectdbpool.excutePreparedStatement(studentinfosql);

		try {
			preparedStatement.setString(1, "yes");
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {

				studentinfo[0] = resultSet.getInt("attend");
				studentinfo[1] = resultSet.getInt("login");
				studentinfo[2] = resultSet.getInt("submit");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Projectdbpool.close();
		}

		return studentinfo;
	}

}
